import axios from "axios";
import { error } from "shelljs";


const request = axios.create({
  baseURL: 'http://localhost:8081/api',
  timeout:5000
})

//request拦截器
//对请求处理
request.interceptors.request.use(config =>{
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    
    let user = localStorage.getItem('user') ?JSON.parse(localStorage('user')) : {};
    
      config.headers['token']  =  localStorage.getItem("token")
   // 设置请求头
      
    return config
  },error => {
    return  Promise.reject(error)
  }
)


//response 拦截器
request.interceptors.response.use(response =>{
  let res = response.data;
  if (typeof res === 'string' && response.headers['content-type'] === 'application/json'){
    res =  JSON.parse(res) 
  }

  return response;
},
error =>{
  console.log('err'+error)
}
)

export default  request
